package com.lex.manager.controller;

import com.lex.manager.pojo.dto.PageParam;
import com.lex.manager.pojo.dto.UserCustomer;
import com.lex.manager.pojo.dto.UserCustomerResult;
import com.lex.manager.pojo.dto.UserQuery;
import com.lex.manager.pojo.po.User;
import com.lex.manager.service.UserService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/{uid}")
    @ResponseBody
    public UserCustomer getUserCustomerByUid(@PathVariable Long uid){
        UserCustomer userCustomer=null;
        try {
            userCustomer=userService.getUserCustomerByUid(uid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return userCustomer;
    }
    @PostMapping("")
    @ResponseBody
    public List<User> listUsers(){
        return userService.listUsers();
    }
    /*@GetMapping("/")
    @ResponseBody
    public UserCustomerResult<UserCustomer> listUserCustomerByPage(PageParam pageParam, UserQuery userQuery){
        UserCustomerResult<UserCustomer> userCustomer=null;
        try {
            userCustomer=userService.listUserCustomerByPage(pageParam,userQuery);
        }catch (Exception e){
            e.printStackTrace();
        }
        return userCustomer;
    }*/
    /*@PostMapping("/")
    @ResponseBody
    public Map<String,Object> insertUser(User user){
        Map<String,Object> insertFlag= new HashMap<>();
        boolean flag=false;
        int rows=0;
        try {
            rows=userService.insertUser(user);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(rows!=0)
            flag=true;
        insertFlag.put("flag",flag);
        return insertFlag;
    }*/
    @DeleteMapping("/{uid}")
    @ResponseBody
    public Map<String,Object> deleteUserByUid(@PathVariable Long uid){
        Map<String,Object> deleteFlag= new HashMap<>();
        boolean flag=false;
        int rows=0;
        try {
            rows=userService.deleteUserByUid(uid);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(rows!=0)
            flag=true;
        deleteFlag.put("flag",flag);
        return deleteFlag;
    }
    @DeleteMapping("/batch")
    @ResponseBody
    public Map<String,Object> deleteBatchByIds(@RequestParam("ids[]")List<Long> ids){
        Map<String,Object> deleteFlag= new HashMap<>();
        boolean flag=false;
        int rows=0;
        try{
            rows=userService.deleteBatchByIds(ids);
            if(rows!=0)
                flag=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        deleteFlag.put("flag",flag);
        return deleteFlag;
    }
}
